package com.ll.studentservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ll.commonUtils.JwtUtils;
import com.ll.commonUtils.MD5;
import com.ll.servicebase.exceptionhandler.LLException;
import com.ll.studentservice.entity.EduInstructor;
import com.ll.studentservice.entity.vo.LoginVo;
import com.ll.studentservice.mapper.EduInstructorMapper;
import com.ll.studentservice.service.EduInstructorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 辅导员 服务实现类
 * </p>
 *
 * @author ll
 * @since 2020-12-07
 */
@Service
public class EduInstructorServiceImpl extends ServiceImpl<EduInstructorMapper, EduInstructor> implements EduInstructorService {

    @Override
    public String login(LoginVo loginVo) {
        //获取登录的手机号和密码
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();

        //手机号和密码非空判断
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)){
            throw new LLException(20001, "手机号或密码为空,登录失败");
        }

        //判断手机号是否正确
        QueryWrapper<EduInstructor> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone);
        EduInstructor eduInstructor = baseMapper.selectOne(wrapper);

        //判断查询出来的对象是否为空
        if (StringUtils.isEmpty(eduInstructor)){
            throw new LLException(20001, "数据库没有这个手机号(测试)");
        }

        System.out.println("传过来的手机号: " + phone);
        System.out.println("数据库的密码" + eduInstructor.getPassword());
        System.out.println("用户输入的密码MD5加密: " + MD5.encrypt(password));

        //判断密码
        //因为存储到数据库的密码肯定加密的
        //把我们输入的密码先进行加密,在和数据库的密码进行比较
        //加密方式 MD5
        if (!MD5.encrypt(password).equals(eduInstructor.getPassword())){
            throw new LLException(20001, "手机号或者密码错误");
        }

        //判断用户是否被禁用

        //登录成功
        //生成token字符串,使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(eduInstructor.getId(), eduInstructor.getName());
        return jwtToken;
    }

}
